package com.bawei.a2010douyin.ui.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.launcher.ARouter;
import com.bawei.a2010douyin.R;
import com.bawei.a2010douyin.adapter.MyFragmentPagerAdapter;
import com.bawei.a2010douyin.base.mvp.BaseFragment;
import com.bawei.a2010douyin.base.utils.ARouterPath;
import com.bawei.a2010douyin.base.utils.GlideUtils;
import com.bawei.a2010douyin.entity.MyTabentity;

import com.bawei.a2010douyin.event.Event;
import com.bawei.a2010douyin.ui.fragment.me.ShouCangFragment;
import com.bawei.a2010douyin.ui.fragment.me.SiMiFragment;
import com.bawei.a2010douyin.ui.fragment.me.ZuoPinFragment;
import com.bawei.a2010douyin.ui.xihuan.XiHuanFragment;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;


public class MeFragment extends BaseFragment {
    private CommonTabLayout common;
    private ArrayList<CustomTabEntity> mTabEntitys = new ArrayList<>();
    private ViewPager viewPager;
    private List<Fragment> mList = new ArrayList<>();
    private MyFragmentPagerAdapter mMyFragmentPagerAdapter;
    private ImageView ivUser;
    private Button btEdit;
    @Override
    public int bindLayout() {
        return R.layout.fragment_me;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        common = (CommonTabLayout) findViewById(R.id.common);
        viewPager = (ViewPager) findViewById(R.id.view_pager);
        ivUser = (ImageView) findViewById(R.id.iv_user);
        btEdit = (Button) findViewById(R.id.bt_edit);
        mTabEntitys.clear();
        mList.clear();
        mTabEntitys.add(new MyTabentity("作品",0,0));
        mTabEntitys.add(new MyTabentity("私密",0,0));
        mTabEntitys.add(new MyTabentity("收藏",0,0));
        mTabEntitys.add(new MyTabentity("喜欢",0,0));
        common.setTabData(mTabEntitys);
        mList.add(new ZuoPinFragment());
        mList.add(new SiMiFragment());
        mList.add(new ShouCangFragment());
        mList.add(new XiHuanFragment());
        mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getChildFragmentManager(), FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT,mList);
        viewPager.setAdapter(mMyFragmentPagerAdapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                common.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        common.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                viewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

        btEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance().build(ARouterPath.EDIT_AROUTER_PATH).navigation();
            }
        });
    }
    //接收方只需要重写此方法即可
    @Override
    public void onEvent(Event event) {
        super.onEvent(event);
        if(event.type == Event.TYPE_ICON){//修改头像
            String data = (String) event.data;
            GlideUtils.getInstance().loadCircleImage(getContext(),data,ivUser);
        }else if(event.type == Event.TYPE_NICKNAME){//修改昵称
            String data = (String) event.data;
            Toast.makeText(getContext(), "修改头像"+data, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void initData() {

    }
}